import CheckAuth from "../../util/Auth"
const {listanzhuang,listfahuo,shouhou,mylist,connect,quxiaojiedan,orderListCount} =require('../../util/api')
// pages/home/home.js
Page({


   
  
  data: {
    datalist:1,
    yijiesuan:"",
    order:'',
    listnum:"",
    dainum:"",
    src:"",
    list:{},
    zongshu:"",
    shouhou:"1",
    num:"",
    index1:"1",
    isPreview:false,
    userId:"",
    dataType:'',
    wokerlist:[],
    pageNum:1,
    userType:'发货',
  },
  //切换待发货状态的订单类型
  ordertype(e){
 wx.redirectTo({
   url: '/pages/lvxinorder/lvxinorder',
 })
  },
  bindHeaderTap4 (e) {  
    this.setData({
      shouhou:e.currentTarget.dataset.type 
     })
     let p={
      userId:wx.getStorageSync('userId'),
      pageNum:1,
      searchName:null,
      workStatus:1
    }
    shouhou(p).then(res=>{
      console.log(res)
      for(let i=0;i< res.data.datas.length;i++){
        let arr1=[];
        arr1= res.data.datas[i].workImgs.split(",")
        res.data.datas[i].arr1=arr1;
      }
      this.setData({
        wokerlist:res.data.datas
      })
      console.log(this.data.wokerlist)
    })
  
  

  },
  bindHeaderTap5 (e) {  
    this.setData({
      shouhou:e.currentTarget.dataset.type 
     })
     let p={
      userId:wx.getStorageSync('userId'),
      pageNum:1,
      searchName:null,
      workStatus:2
    }
    shouhou(p).then(res=>{
      console.log(res)
      for(let i=0;i< res.data.datas.length;i++){
        let arr1=[];
        arr1= res.data.datas[i].workImgs.split(",")
        res.data.datas[i].arr1=arr1;
      }
      this.setData({
        wokerlist:res.data.datas
      })
      console.log(this.data.wokerlist)
    })
  

  },
  new(e){
    this.setData({
      index1:e.currentTarget.dataset.index1
    })

    
  },
  finish(e){
    console.log(e)
    this.setData({
      id:e.currentTarget.dataset.id
    })
    wx.navigateTo({
      url: '/pages/weixiu/weixiu?id='+this.data.id,
    })
   
  },

  old(e){
    this.setData({
      index1:e.currentTarget.dataset.index1
    })
    let p={
      userId:wx.getStorageSync('userId'),
      pageNum:1,
      searchName:null,
      workStatus:1
    }
    shouhou(p).then(res=>{
      console.log(res)
      for(let i=0;i< res.data.datas.length;i++){
        let arr1=[];
        arr1= res.data.datas[i].workImgs.split(",")
        res.data.datas[i].arr1=arr1;
      }
      
      this.setData({
        wokerlist:res.data.datas
      })
      console.log(this.data.wokerlist)
    })

    
  },
  bindHeaderTap0 (e) {  
    console.log('这是待安装')
    this.setData({
      dataType:e.currentTarget.dataset.type,
      pageNum:1
     })
    let params={
      userId:wx.getStorageSync('userId'),
      pageNum:this.data.pageNum,
      searchName:'',
      payStatus:5 
    };
    listanzhuang(params).then(res=>{
     console.log(res)
      for(let i=0;i< res.data.data.length;i++){
        let arr1=[];
        arr1= res.data.data[i].aaEquipmentType.etImgs.split(",")
        res.data.data[i].aaEquipmentType.arr1=arr1;
       
      }
     
       this.setData({
         list:res.data.data,
        
        })

  })},
  
  
  bindHeaderTap2 (e) {  
    this.setData({
      dataType:e.currentTarget.dataset.type,
      pageNum:1

     })
    let params={
      userId:wx.getStorageSync('userId'),
      pageNum:this.data.pageNum,
      searchName:'',
      payStatus:e.currentTarget.dataset.type 
    };
    listanzhuang(params).then(res=>{
      console.log(res)
      for(let i=0;i< res.data.data.length;i++){
        let arr1=[];
        arr1= res.data.data[i].aaEquipmentType.etImgs.split(",")
        res.data.data[i].aaEquipmentType.arr1=arr1;
       
      }
     
       this.setData({
         list:res.data.data,
        
        })

  })},
  connect(e){
    let p={
      userId:wx.getStorageSync('userId'),
      orderId:e.currentTarget.dataset.orderno
    }
    let that=this
    wx.showModal({
      title: '提示',
      content: '确定接单吗？',
      success (res) {
        if (res.confirm) {
          connect(p).then(res=>{
            if(res.code==200){
              wx.showToast({
                title: '接单成功',
                icon: 'success',
                duration: 2000
              })
              that.onShow()
            }
          })
          
         
        } else if (res.cancel) {
          wx.showToast({
            title: '未接单',
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  quxiao(e){
    console.log(e)
    let p={
      id:e.currentTarget.dataset.data,
      orderId:e.currentTarget.dataset.orderno,
      userId:wx.getStorageSync('userId')
    }
    let that=this
    wx.showModal({
      title: '提示',
      content: '取消接单',
      success (res) {
        if (res.confirm) {
          quxiaojiedan(p).then(res=>{
           if(res.code==200){
            wx.showToast({
              title: '已取消',
              icon: 'success',
              duration: 2000
            })
            let params={
              userId:wx.getStorageSync('userId'),
              pageNum:that.data.pageNum,
              searchName:'',
              payStatus:5 
            };
            listanzhuang(params).then(res=>{
             console.log(res)
              for(let i=0;i< res.data.data.length;i++){
                let arr1=[];
                arr1= res.data.data[i].aaEquipmentType.etImgs.split(",")
                res.data.data[i].aaEquipmentType.arr1=arr1;
              }
             
               that.setData({
                 list:res.data.data,
                
                })
                that.onShow()
        
          })
           }else{
            that.onShow()
           }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        
        }
      }
    })
    console.log(e)
  },
  bindHeaderTap3(e){
    this.setData({
      dataType:e.currentTarget.dataset.type,
      pageNum:1
     })
    let params={
      userId:wx.getStorageSync('userId'),
      pageNum:this.data.pageNum,
      searchName:'',
      payStatus:e.currentTarget.dataset.type 
    };
   
    listanzhuang(params).then(res=>{
      
     console.log(res)
     for(let i=0;i< res.data.data.length;i++){
      let arr1=[];
      arr1= res.data.data[i].aaEquipmentType.etImgs.split(",")
      res.data.data[i].aaEquipmentType.arr1=arr1;
      let arr=[];
      if(res.data.data[i].imgs!=null){
        arr= res.data.data[i].imgs.split(",")
        res.data.data[i].arr1=arr;
      }
      else{
        res.data.data[i].arr1=arr;
      }
    
    }
  
       this.setData({
         list:res.data.data,
         zongshu:  res.data.data.length
        })
       
  })
  },
  bindHeaderTap (e) {   
    console.log(e)
    this.setData({
      dataType:e.currentTarget.dataset.type 
     })
    let params={
      userId:wx.getStorageSync('userId'),
      pageNum:this.data.pageNum,
      searchName:'',
      payStatus:e.currentTarget.dataset.type 
    };
    listfahuo(params).then(res=>{
      console.log(res)
      for(let i=0;i< res.data.data.datas.length;i++){
        let arr=[];
        arr= res.data.data.datas[i].aaEquipmentType.etImgs.split(",")
        res.data.data.datas[i].aaEquipmentType.arr1=arr;
       
      }
       this.setData({
         list:res.data.data.datas,
         
        })
        console.log(this.data.list)

  })},
  bindHeaderTap1(e){
  
    this.setData({
      dataType:e.currentTarget.dataset.type 
     })
    let params={
      userId:wx.getStorageSync('userId'),
      pageNum:this.data.pageNum,
      searchName:'',
      payStatus:e.currentTarget.dataset.type 
    };
   
    listfahuo(params).then(res=>{
      for(let i=0;i< res.data.data.datas.length;i++){
        let arr=[];
        arr= res.data.data.datas[i].aaEquipmentType.etImgs.split(",")
        res.data.data.datas[i].aaEquipmentType.arr1=arr;
       
      }
     if(e.currentTarget.dataset.type==1){
      this.setData({
        list:res.data.data.datas    
       })
     }
    if(e.currentTarget.dataset.type==2){
      this.setData({
        list:res.data.data.datas  ,
        listnum: res.data.data.datas.length    
       })

     }
       
  })
  },
  handtap(e){
    this.setData({isPreview: true})
    for (var i=0; i < e.currentTarget.dataset.src.length; i++) {
        e.currentTarget.dataset.src[i]='https://anjingbao.anjingbao.net/prod-api'+e.currentTarget.dataset.src[i]
     }
     let src1='https://anjingbao.anjingbao.net/prod-api'+e.currentTarget.dataset.src[0];
  
     wx.previewImage({
     current:src1,
     urls: e.currentTarget.dataset.src
     
    })
    
  // console.log(e)
  },
 
  

  send(option){
   const orderId = option.currentTarget.dataset.orderno
   const detailedid = option.currentTarget.dataset.detailed
   const address = option.currentTarget.dataset.address
    wx.navigateTo({
     url: this.data.userType == '发货' ? "../home/fahuo/fahuo?id="+orderId  + "&address=" + JSON.stringify(address): "../anzhuang/anzhuang?id="+orderId+"&no="+detailedid,
    })
   },
    

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let type=wx.getStorageSync('userType')
    if(type=='22'){
      this.setData({
        dataType:2
       })
      let params={
        userId:wx.getStorageSync('userId'),
        pageNum:this.data.pageNum,
        searchName:'',
        payStatus:2 
      };
        // 请求安装端订单列表接口
        listanzhuang(params).then(res=>{
         console.log(res)
          if (res.code ===200){
            
            for(let i=0;i< res.data.data.length;i++){
              let arr=[];
              arr= res.data.data[i].aaEquipmentType.etImgs.split(",")
              res.data.data[i].aaEquipmentType.arr1=arr;
            }
            this.setData({
              list:res.data.data,
             
              userType:'安装',
              dataType:2,
              order:res.data.data.length,
              yijiesuan:res.data.isFinish
            })
           
            let p={
              userId:wx.getStorageSync('userId'),
              pageNum:1,
              searchName:null,
              workStatus:1
            }
            shouhou(p).then(res=>{
              console.log(res)
              for(let i=0;i< res.data.datas.length;i++){
                let arr1=[];
                arr1= res.data.datas[i].workImgs.split(",")
                res.data.datas[i].arr1=arr1;
              }
              
              this.setData({
                wokerlist:res.data.datas
              })
            
            })       
          }else{
            wx.showToast({
              title: res.msg,
              icon:"none",
              duration: 5000
            })
          }
        })
         
        let params3={
          userId:wx.getStorageSync('userId'),
          pageNum:this.data.pageNum,
          searchName:'',
          payStatus:3 
        };
          // 请求安装端订单列表接口
          listanzhuang(params3).then(res=>{
           console.log("onshow里获取安装订单列表",res)
            if (res.code ===200){
              
              for(let i=0;i< res.data.data.length;i++){
                let arr=[];
                arr= res.data.data[i].aaEquipmentType.etImgs.split(",")
                res.data.data[i].aaEquipmentType.arr1=arr;
              }
              this.setData({
                listnum: res.data.data.length
               })
            }})
        let pa={
          userId:wx.getStorageSync('userId'),
          pageNum:this.data.pageNum,
          searchName:'',
          payStatus:5 
        };
        listanzhuang(pa).then(res=>{
         console.log(res)
          for(let i=0;i< res.data.data.length;i++){
            let arr1=[];
            arr1= res.data.data[i].aaEquipmentType.etImgs.split(",")
            res.data.data[i].aaEquipmentType.arr1=arr1;
           
          }
         
           this.setData({
             dainum:res.data.data.length,
            
            })
    
      })

    }else
      if(type=='33')
     {
        console.log("这是发货端")
      this.setData({
        dataType:1
       })
       let pa={
        userId:wx.getStorageSync('userId'),
      }
      orderListCount(pa).then(res=>{
   
        this.setData({
          eqnum:res.data.dataCount1,
          lvxinnum:res.data.dataCountlvxin1
        })
      })
      let params={
        userId:wx.getStorageSync('userId'),
        pageNum:this.data.pageNum,
        searchName:'',
        payStatus:1 
      };
       // 请求发货端订单列表接口
       listfahuo(params).then(res=>{
         console.log(res)
        if (res.code ===200){
          for(let i=0;i< res.data.data.datas.length;i++){
            let arr=[];
            arr= res.data.data.datas[i].aaEquipmentType.etImgs.split(",")
            res.data.data.datas[i].aaEquipmentType.arr1=arr;
           
          }
          this.setData({
            list:res.data.data.datas,
            userType:'发货',
           })
        }else{
          wx.showToast({
            title: res.msg,
            icon:"none",
            duration: 5000
          })
        }
      })


      let params2={
        userId:wx.getStorageSync('userId'),
        pageNum:this.data.pageNum,
        searchName:'',
        payStatus:2 
      };
       // 请求发货端订单列表接口
       listfahuo(params2).then(res=>{
         console.log(res)
        if (res.code ===200){
          this.setData({
            listnum: res.data.data.datas.length
           })
        }else{
          wx.showToast({
            title: res.msg,
            icon:"none",
            duration: 5000
          })
        }
      })
    }

  } ,
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    this.setData({
      userId : wx.getStorageSync('userId')
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    console.log('到底了')
    this.setData({
      pageNum: this.data.pageNum+1
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})